<template>
  <div class="product-list">
    <div class="page-header">
      <h2>商品管理</h2>
      <router-link to="/shopkeeper/products/add" class="add-btn">
        添加商品
      </router-link>
    </div>
    
    <div v-if="loading" class="loading">加载中...</div>
    <div v-else-if="error" class="error">{{ error }}</div>
    <div v-else-if="products.length === 0" class="empty">
      暂无商品，点击上方按钮添加商品
    </div>
    
    <div v-else class="products-grid">
      <div v-for="product in products" :key="product.id" class="product-card">
        <img :src="product.image" :alt="product.name" class="product-image" />
        <div class="product-info">
          <h3>{{ product.name }}</h3>
          <p class="product-description">{{ product.description }}</p>
          <div class="product-meta">
            <span class="price">¥{{ product.price }}</span>
            <span class="stock">库存: {{ product.stock }}</span>
            <span class="category">{{ product.category }}</span>
          </div>
          <div class="product-actions">
            <router-link 
              :to="`/shopkeeper/products/edit/${product.id}`" 
              class="edit-btn"
            >
              编辑
            </router-link>
            <button @click="deleteProduct(product.id)" class="delete-btn">
              删除
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { storeToRefs } from 'pinia'
import { useProductStore } from '../../stores/product'

const productStore = useProductStore()
const { products, loading, error } = storeToRefs(productStore)

onMounted(() => {
  productStore.fetchProducts()
})

const deleteProduct = async (productId) => {
  if (confirm('确定要删除这个商品吗？')) {
    try {
      await productStore.deleteProduct(productId)
      alert('商品删除成功！')
    } catch (err) {
      alert('删除商品失败：' + err.message)
    }
  }
}
</script>

<style scoped>
.product-list {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
  color: #333;
}

.add-btn {
  padding: 10px 20px;
  background: #667eea;
  color: white;
  text-decoration: none;
  border-radius: 5px;
  font-weight: 500;
  transition: background-color 0.3s;
}

.add-btn:hover {
  background: #5a6fd8;
}

.loading, .error, .empty {
  text-align: center;
  padding: 40px;
  font-size: 16px;
}

.error {
  color: #e74c3c;
}

.empty {
  color: #666;
}

.products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.product-card {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.2s, box-shadow 0.2s;
}

.product-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.product-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.product-info {
  padding: 15px;
}

.product-info h3 {
  margin: 0 0 10px 0;
  color: #333;
  font-size: 18px;
}

.product-description {
  color: #666;
  font-size: 14px;
  margin-bottom: 15px;
  line-height: 1.4;
}

.product-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.price {
  color: #ff4757;
  font-size: 18px;
  font-weight: bold;
}

.stock {
  color: #666;
  font-size: 14px;
}

.category {
  background: #f0f2ff;
  color: #667eea;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.product-actions {
  display: flex;
  gap: 10px;
}

.edit-btn, .delete-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  text-decoration: none;
  text-align: center;
  transition: background-color 0.3s;
}

.edit-btn {
  background: #667eea;
  color: white;
}

.edit-btn:hover {
  background: #5a6fd8;
}

.delete-btn {
  background: #ff4757;
  color: white;
}

.delete-btn:hover {
  background: #ff3742;
}
</style>